//
//  HWHomeController.swift
//  HWPromoteProjectAPP
//
//  Created by 王昌健 on 2022/1/24.
//  Copyright © 2022 王昌健. All rights reserved.
//

import UIKit
import SwiftUI

class HWHomeController: HWBaseViewController {
    
    var topBgImageView:UIImageView!
    var headImageView:UIImageView!
    var nameLabel:UILabel!
    var currentLoginLabel:UILabel!
    var qrCodeButton:UIButton!
    var collectionView:UICollectionView!
    let collectionProtocol = HWHomeProtocol()
    let httpManager = HWHttpManager()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        loadData()
        
        addRefresh()
        
        addAction()
        
    }

}

extension HWHomeController {
    private func initUI() -> Void {
        navigationView.isHidden = true
        
        topBgImageView = UIImageView()
        topBgImageView.image = UIImage(named: "homeTopBg")
        view.addSubview(topBgImageView)
        
        headImageView = UIImageView()
        headImageView.layer.cornerRadius = 10*widthScale
        headImageView.backgroundColor = .red
        headImageView.sd_setImage(with: URL(string: UserDefaults.standard.object(forKey: "head") as! String), completed: nil)
        topBgImageView.addSubview(headImageView)
        
        nameLabel = HWLabel.createBoldLabel(title: UserDefaults.standard.object(forKey: "username") as! String, color: "#FFFFFF", font: 14*widthScale)
        topBgImageView.addSubview(nameLabel)
        
        currentLoginLabel = HWLabel.createLabel(title: "当前登录", color: "#FFFFFF", font: 10*widthScale)
        topBgImageView.addSubview(currentLoginLabel)
        
        qrCodeButton = UIButton(type: .custom)
        qrCodeButton.setImage(UIImage(named: "scan"), for: .normal)
        qrCodeButton.addTarget(self, action: #selector(qrCodeButtonClick(_:)), for: .touchUpInside)
        topBgImageView.isUserInteractionEnabled = true
        topBgImageView.addSubview(qrCodeButton)
        qrCodeButton.isHidden = true
        
        let flowL = UICollectionViewFlowLayout.init()
        flowL.minimumLineSpacing = 0
        flowL.minimumInteritemSpacing = 0
        
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
        collectionView.delegate = collectionProtocol
        collectionView.dataSource = collectionProtocol
        collectionView.showsVerticalScrollIndicator = false
        collectionView.contentInsetAdjustmentBehavior = .never
        collectionView.backgroundColor = .white
        collectionView.register(HWHomeFunctionCell.self, forCellWithReuseIdentifier: "HWHomeFunctionCell")
        collectionView.register(HWHomeDataCell.self, forCellWithReuseIdentifier: "HWHomeDataCell")
        collectionView.register(HWHomeTopThreeRankCell.self, forCellWithReuseIdentifier: "HWHomeTopThreeRankCell")
        collectionView.register(HWHomeOtherRankCell.self, forCellWithReuseIdentifier: "HWHomeOtherRankCell")
        collectionView.register(HWHomeHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "HWHomeHeader")
//        collectionView.register(HWGameCircleFooter.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "HWGameCircleFooter")
        view.addSubview(collectionView)
    }
    
    private func initFrame() -> Void {
        topBgImageView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(91*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(20*widthScale)
            make.width.height.equalTo(33*widthScale)
            make.top.equalTo(50*widthScale)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(4*widthScale)
            make.top.equalTo(headImageView)
        }
        
        currentLoginLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView).offset(-2*widthScale)
        }
        
        qrCodeButton.snp.makeConstraints { make in
            make.right.equalTo(-21*widthScale)
            make.centerY.equalTo(headImageView)
            make.width.height.equalTo(18*widthScale)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(topBgImageView.snp.bottom)
        }
    }
}

extension HWHomeController {
    func loadData() -> Void {
        httpManager.getHomeInfo { infoModel in
            self.collectionView.mj_header?.endRefreshing()
            self.collectionProtocol.model = infoModel
            self.collectionView.reloadData()
        } failure: { error in
            self.collectionView.mj_header?.endRefreshing()
            self.view.showHUD(title: error)
        }
    }
    
    func addRefresh() -> Void {
        collectionView.mj_header = MJRefreshNormalHeader()
        collectionView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
    }
    
    @objc func loadNewData() -> () {
        loadData()
    }
}

extension HWHomeController {
    func addAction() -> Void {
        collectionProtocol.didClickNav = {
            mod in
            switch HWMineNavType(rawValue: mod.type) {
            case .none: break
            /// 推广盒子
            case .some(.promoteBox):
                let qrCode = HWPromotionQrCodeViewController()
                self.navigationController?.pushViewController(qrCode, animated: true)
            /// 推广游戏
            case .some(.promoteGame):break
            /// 我的游戏
            case .some(.myGame):
//                let promoteList = HWPromoteListController()
//                promoteList.hideNav = false
//                self.navigationController?.pushViewController(promoteList, animated: true)
                let searchGame = HWPromoteSearchController()
                self.navigationController?.pushViewController(searchGame, animated: true)
            /// 申请游戏
            case .some(.applyGame):
                self.navigationController?.pushViewController(HWApplyGameViewController(), animated: true)
            /// 折扣代充
            case .some(.saleRecharge):
                self.navigationController?.pushViewController(HWDiscountDaiChunViewController(), animated: true)
            /// 申请结算
            case .some(.applySettlement):
                self.navigationController?.pushViewController(HWApplyjiesuanViewController(), animated: true)
            /// 结算明细
            case .some(.settlementDetailed):
                self.navigationController?.pushViewController(HWBillingDetailsViewController(), animated: true)
            /// 分成明细
            case .some(.divisionDetails):
                self.navigationController?.pushViewController(HWDividedIntoViewController(), animated: true)
            /// 下线管理
            case .some(.subordinate):
                self.navigationController?.pushViewController(MyOfflineViewController(), animated: true)
            /// 提现管理
            case .some(.withdrawalManage):
                self.navigationController?.pushViewController(HWTixianguanliViewController(), animated: true)
            /// 添加下线
            case .some(.addSubordinate):
                self.navigationController?.pushViewController(HWtianjiangxianxiangController(), animated: true)
            /// 分成比例
            case .some(.divideScale):
                self.navigationController?.pushViewController(HWDividedintoProportionViewController(), animated: true)
            /// 开服信息
            case .some(.openServiceInfo):
                self.navigationController?.pushViewController(HWOpenServiceInfoController(), animated: true)
            /// 游戏礼包
            case .some(.gameGiftBag):
                self.navigationController?.pushViewController(HWGameGiftBagController(), animated: true)
            /// 注册统计
            case .some(.registerStatistics):
                self.navigationController?.pushViewController(HWRegistrationStatisticsViewController(), animated: true)
            /// 充值统计
            case .some(.rechargeStatistics):
                self.navigationController?.pushViewController(HWTopupStatisticsViewController(), animated: true)
            /// 角色汇总
            case .some(.roleSummary):
                self.navigationController?.pushViewController(HWRoleSummaryViewController(), animated: true)
            /// 注册方式
            case .some(.registType):
                let typeController = HWRegistTypeController()
                typeController.type = .registType
                self.navigationController?.pushViewController(typeController, animated: true)
            /// 充值方式
            case .some(.rechargeType):
                let typeController = HWRegistTypeController()
                typeController.type = .rechargeType
                self.navigationController?.pushViewController(typeController, animated: true)
            /// 消费榜单
            case .some(.consumeRank):
                let rankList = HWRankListController()
                rankList.type = .consumeRank
                self.navigationController?.pushViewController(rankList, animated: true)
            /// 推广排行
            case .some(.promoteRank):
                self.navigationController?.pushViewController(HWtuiguanpanhangbangViewController(), animated: true)
            /// 个人信息
            case .some(.mineInfo):
                self.navigationController?.pushViewController(HWPersonalInformationViewController(), animated: true)
            /// 实名认证
            case .some(.realNameAuthentication):
                let my = MyViewController()
                my.navController = self.navigationController!
                my.shifoshimingrenzheng()
            /// 修改密码
            case .some(.changePassword):
                self.navigationController?.pushViewController(HWChangeThePasswordViewController(), animated: true)
            /// 充值记录
            case .some(.rechargeRecord):
                self.navigationController?.pushViewController(HWPrepaidPhoneRecordsjiluViewController(), animated: true)
            /// 提现明细
            case .some(.withdrawalDetails):break
            /// 联系我们
            case .some(.contactUs):
                self.navigationController?.pushViewController(HWContactUsViewController(), animated: true)
            /// 人气榜
            case .some(.popularityRank):
                let rankList = HWRankListController()
                rankList.type = .popularityRank
                self.navigationController?.pushViewController(rankList, animated: true)
            /// 推广公告
            case .some(.promoteNotice):
                let promoteNotice = HWPromotionAnnouncementViewController()
                self.navigationController?.pushViewController(promoteNotice, animated: true)
            }
        }
    }
    
    @objc func qrCodeButtonClick(_:UIButton) -> Void {
        let scan  = HWScanViewController()
        var style = LBXScanViewStyle()
        style.centerUpOffset = 44
        style.photoframeAngleStyle = LBXScanViewPhotoframeAngleStyle.Inner
        style.photoframeLineW = 2
        style.photoframeAngleW = 18
        style.photoframeAngleH = 18
        style.isNeedShowRetangle = false
        style.colorAngle = UIColor(red: 0.0/255, green: 200.0/255.0, blue: 20.0/255.0, alpha: 1.0)
        style.anmiationStyle = .LineMove
        style.animationImage = UIImage(named: "qrcode_Scan_weixin_Line")
        scan.scanStyle = style
        self.navigationController?.pushViewController(scan, animated: true)
    }
}
